在 clipboard 存取剪貼簿的 api 中,拿到的是一個 promise type 的值,所以也需要 .then 來解讀:
navigator.clipboard.readText().then( text => {
console.log(text)
})
const api500 = 'https://run.mocky.io/v3/74e65703-c9b2-4c4c-b36e-88d8cc6cd253'
const api200 = 'https://run.mocky.io/v3/d2381372-4e30-4ec6-8a03-c3573417112d'
const data = {
name: 'rock070'
}
const request = fetch(api200, {
method: "POST",
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json'
}),
})
request.then( res => {
console.log(res)
return res.json()
})
.catch(error => console.log('Error:', error))
.then( response => console.log('Success:', response));
參考資料:clipboard 讀取剪貼簿文字 API - by MDN Web Docs
function init(resolve, reject) {
resolve({
init: "3",
name: "rock070",
location: 'Louisa'
})
reject('errorCode')
}
const myPromise = new Promise(init)
// Promise 的引數要是一個 function
myPromise.then(res => console.log(res))
.catch(err => console.log('err', err))
const myPromise = new Promise((resolve, reject) => {
setTimeout( () => {
resolve()
reject()
}, 3000)
})
myPromise.then(data => console.log('data', data))
.catch(err => console.log('err', err))
設定 3 秒後 resolve!